<template>
  <ModuleContainer :moduleBase="module.attrs">
    <!-- 1行布局 -->
    <div
      class="row1"
      v-show="module.attrs.row == 1"
      :style="{ '--swiper-dot-color': module.attrs.dotColor }"
    >
      <swiper style="width: 100%; height: 100%" ref="mySwiper" :options="swiperOption">
        <swiper-slide
          class="pageItem"
          v-for="(item1, index1) in Math.ceil(
            module.list.length / module.attrs.col
          )"
          :key="item1.id"
        >
          <div
            class="navItemBox"
            :style="{ width: 100 / module.attrs.col + '%' }"
            v-for="(item2, index2) in getPageList(index1, 1, module.attrs.col)"
            :key="item1.id"
          >
            <div
              class="navItem"
              @click="
                $util.openAttr(module.list, index1 * module.attrs.col + index2)
              "
            >
              <template>
                <img
                  class="item-nav-img"
                  :class="{ 'round': module.attrs.circle }"
                  :src="item2.img.url"
                  v-if="item2.img.imgType == 3 && item2.img.url"
                />
                <img
                  class="item-nav-img"
                  :class="{ 'round': module.attrs.circle }"
                  src="../../assets/imgs/defImg.jpg"
                  v-else
                />
              </template>
              <div class="item-nav-text">
                <textParse :textAttr="item2.title" :defStyle="titleDefStyle" />
              </div>
            </div>
          </div>
        </swiper-slide>
        <div
          class="swiper-pagination"
          slot="pagination"
          v-show="module.list.length > module.attrs.col"
        ></div>
      </swiper>
    </div>
    <!-- 2行布局 -->
    <div
      class="row2"
      v-show="module.attrs.row == 2"
      :style="{ '--swiper-dot-color': module.attrs.dotColor }"
    >
      <swiper style="width: 100%; height: 100%" ref="mySwiper" :options="swiperOption">
        <swiper-slide
          class="pageItem"
          v-for="(item1, index1) in Math.ceil(
            module.list.length / (module.attrs.row * module.attrs.col)
          )"
          :key="index1"
        >
          <div
            class="navItemBox"
            :style="{ width: 100 / module.attrs.col + '%' }"
            v-for="(item2, index2) in getPageList(index1, 2, module.attrs.col)"
            :key="index2"
          >
            <div
              class="navItem"
              @click="
                $util.openAttr(
                  module.list,
                  index1 * module.attrs.col * 2 + index2
                )
              "
            >
              <template v-if="item2.img.url != 2">
                <img
                  class="item-nav-img"
                  :class="{ 'round': module.attrs.circle }"
                  :src="item2.img.url"
                  v-if="item2.img.imgType == 3 && item2.img.url"
                />
                <img
                  class="item-nav-img"
                  :class="{ 'round': module.attrs.circle }"
                  src="../../assets/imgs/defImg.jpg"
                  v-else
                />
              </template>
              <div class="item-nav-text">
                <textParse :textAttr="item2.title" :defStyle="titleDefStyle" />
              </div>
            </div>
          </div>
        </swiper-slide>
        <div
          class="swiper-pagination"
          slot="pagination"
          v-show="module.list.length > module.attrs.row * module.attrs.col"
        ></div>
      </swiper>
    </div>
  </ModuleContainer>
</template>
<script>
import { Swiper, SwiperSlide, directive } from "vue-awesome-swiper";
import "swiper/css/swiper.css";
import "swiper/swiper.scss";
// 此模块为图文导航模块，上面是图片，下面是文字，多用于首页导航，配合自定义链接使用即可实现跳转
export default {
  name: "navigation",
  props: ["module"],
  components: { Swiper, SwiperSlide },
  directives: { swiper: directive },
  data() {
    return {
      nav: [],
      swiperOption: {
        autoplay: 1000,
        observer: true,
        observeParents: true,
        pagination: {
          el: ".swiper-pagination",
          bulletClass: "my-bullet",
          bulletActiveClass: "my-bullet-active",
        }
      }
    };
  },

  computed: {
    titleDefStyle() {
      if (this.module.attrs.fontColor != "") {
        return {
          fontColor: this.module.attrs.fontColor || "#999",
          fontSize: 11,
        };
      }
    },
  },
  methods: {
    getPageList(index, row, col) {
      let list = [];
      if (index == 0) {
        list = this.module.list.slice(0, row * col);
      } else {
        list = this.module.list.slice(
          index * row * col,
          (index + 1) * row * col
        );
      }
      return list;
    },
  },
};
</script>

<style lang="scss">
.row1 {
  width: 100%;
  height: 110px;
  padding: 0 0 10px 0;
}
.row2 {
  width: 100%;
  height: 190px;
  padding: 0 0 10px 0;
}
.row1,
.row2 {
  $swiper-dot-color: var(--swiper-dot-color);
  .my-bullet-active {
    background: $swiper-dot-color;
  }
}
.round {
  border-radius: 50%;
}
.pageItem {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
  height: 80px;
  .navItemBox {
    height: 80px;
    padding: 10px 10px 0;
    .navItem {
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      align-items: center;
      width: 100%;
      height: 80px;
      img {
        width: 55px;
        height: 55px;
        min-width: 55px;
        min-height: 55px;
      }
    }
  }
}
.swiper-container {
  // --swiper-theme-color: #ff6600;
  // --swiper-pagination-color: #00ff33;/* 两种都可以 */
}
.swiper-pagination-bullets {
  bottom: 0px !important;
}
.my-bullet {
  width: 1.2rem;
  height: 0.3rem;
  display: inline-block;
  // border-radius: 50%;
  background: #b7b6b9;
  &:nth-child(1) {
    border-top-left-radius: 2px;
    border-bottom-left-radius: 2px;
  }
  &:nth-last-child(1) {
    border-top-right-radius: 2px;
    border-bottom-right-radius: 2px;
  }
}
</style>
